package com.da.javatest.interview.session3;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author JokerDa.
 * @date 2020/12/27 15:25
 * Description:等待唤醒的3种方式
 * 1.Object中的wait() notify()  2.Condition中的await() signal()  3.LockSupport park() unpark()
 * 第1种，不能脱离synchronized代码块，wait()必须在notify()前
 * 第2种，不能脱离lock unlock代码块，await()必须在signal()前
 * 第3种，无须代码块限制，也无须先后顺序限制，但是许可证最大为1
 */
public class Test04LockSupport {
    private static final Object objectLock = new Object();
    private static final Lock lock = new ReentrantLock();
    private static final Condition condition = lock.newCondition();

    public static void main(String[] args) {
        //m1();
        //m2();
        m3();
    }

    private static void m1() {
        new Thread(() -> {
            //try {
            //    Thread.sleep(2000);
            //} catch (InterruptedException e) {
            //    e.printStackTrace();
            //}
            synchronized (objectLock) {
                System.out.println(Thread.currentThread().getName() + " come in");
                try {
                    objectLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " come out");
            }
        }, "A").start();

        new Thread(() -> {
            synchronized (objectLock) {
                System.out.println(Thread.currentThread().getName() + " 唤醒线程");
                objectLock.notify();
            }
        }, "B").start();
    }

    private static void m2() {
        new Thread(() -> {
            try {
                //Thread.sleep(2000);
                lock.lock();
                System.out.println(Thread.currentThread().getName() + " come in");
                condition.await();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            System.out.println(Thread.currentThread().getName() + " come out");
        }, "A").start();

        new Thread(() -> {
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + " 唤醒线程");
                condition.signal();
            } finally {
                lock.unlock();
            }
        }, "B").start();
    }

    private static void m3() {
        Thread a = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " come in");
            LockSupport.park();
            //LockSupport.park();
            System.out.println(Thread.currentThread().getName() + " come out");
        }, "A");
        a.start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " 唤醒线程");
            LockSupport.unpark(a);
            //LockSupport.unpark(a);
        }, "B").start();
    }
}
